package com.qdl.rym.system.controller;

import com.qdl.rym.base.BaseController;
import com.qdl.rym.base.ResultModel;
import com.qdl.rym.exception.BusinessException;
import com.qdl.rym.system.dto.AlbumDTO;
import com.qdl.rym.system.po.Album;
import com.qdl.rym.system.service.AlbumService;
import com.qdl.rym.util.ListUtil;
import com.qdl.rym.util.RegexUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description: AlbumController
 * @Author: ChenXueLi
 * @Date: 2019/3/19 14:16
 * 你是一段成熟的代码，要学会自己改bug
 */
@Scope(value = "prototype")
@Slf4j
@Api(tags = "相册管理")
@RestController
@RequestMapping("/album")
public class AlbumController extends BaseController {

    @Autowired
    private AlbumService albumService;

    /**
     * 上传信息
     * @param title
     * @param describe
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "上传信息", httpMethod = "POST", response = Album.class)
    @RequestMapping(value = "/addAlbum", method = RequestMethod.POST)
    public ResultModel addAlbum(
            @ApiParam(value = "文件id（多个用','分隔）", required = true) @RequestParam String fileIds,
//            @ApiParam(value = "作品文件", required = true) @RequestParam MultipartFile file,
            @ApiParam(value = "标题", required = true) @RequestParam String title,
            @ApiParam(value = "描述", required = true) @RequestParam String describe
    ) throws Exception{
//        if(file == null) {
//            throw new BusinessException(-101, "作品文件不能为空");
//        }
        List<Integer> idList = ListUtil.getInstance().stringToIntegerList(fileIds);
        if(idList.size() == 0) {
            throw new BusinessException(-102, "文件不能为空");
        }
        if(RegexUtil.isNull(title)) {
            throw new BusinessException(-102, "标题不能为空");
        }
        if(title.length() > 25) {
           title = title.substring(25);
        }
        if(RegexUtil.isNull(describe)) {
            throw new BusinessException(-103, "描述信息不能为空");
        }
        if(describe.length() > 500) {
            describe = describe.substring(500);
        }

        Album album = new Album();
        album.initModel();
        album.setTitle(title);
        album.setDescribe(describe);
        albumService.addAlbum(album, idList, resultModel);
        return resultModel;
    }

    /**
     * 修改信息
     * @param albumId
     * @param title
     * @param describe
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改信息", httpMethod = "POST", response = Album.class)
    @RequestMapping(value = "/updateAlbum", method = RequestMethod.POST)
    public ResultModel updateAlbum(
            @ApiParam(value = "id", required = true) @RequestParam Integer albumId,
            @ApiParam(value = "文件id（多个用','分隔）", required = true) @RequestParam String fileIds,
//            @ApiParam(value = "作品文件", required = false) @RequestParam MultipartFile file,
            @ApiParam(value = "标题", required = false) @RequestParam String title,
            @ApiParam(value = "描述", required = false) @RequestParam String describe
    ) throws Exception{
        if(albumId <= 0) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        List<Integer> idList = ListUtil.getInstance().stringToIntegerList(fileIds);
        if(idList.size() == 0) {
            throw new BusinessException(-102, "文件不能为空");
        }
        if(RegexUtil.isNotNull(title) && title.length() > 25) {
            title = title.substring(25);
        }
        if(RegexUtil.isNotNull(describe) && describe.length() > 500) {
            describe = describe.substring(500);
        }

        Album album = new Album();
        album.initModel();
        album.setAlbumId(albumId);
        album.setTitle(title);
        album.setDescribe(describe);

        albumService.updateAlbum(album, idList, resultModel);
        return resultModel;
    }

    /**
     * 删除信息
     * @param albumId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除信息", httpMethod = "POST", response = Album.class)
    @RequestMapping(value = "/deleteAlbum", method = RequestMethod.POST)
    public ResultModel deleteAlbum(
            @ApiParam(value = "id", required = true) @RequestParam Integer albumId
    ) throws Exception{
        if(albumId <= 0) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        albumService.deleteAlbum(albumId, resultModel);
        return resultModel;
    }

    /**
     * 根据id查询信息
     * @param albumId
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据id查询信息", httpMethod = "POST", response = Album.class)
    @RequestMapping(value = "/getAlbumById", method = RequestMethod.POST)
    public ResultModel getAlbumById(
            @ApiParam(value = "id", required = true) @RequestParam Integer albumId
    ) throws Exception{
        if(albumId <= 0) {
            throw new BusinessException(-101, "未查询到指定的信息");
        }
        albumService.getAlbumById(albumId, resultModel);
        return resultModel;
    }

    /**
     * 查询列表信息（分页）
     * @param rows
     * @param page
     * @param keywords
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询列表信息（分页）", httpMethod = "GET", response = AlbumDTO.class)
    @RequestMapping(value = "/getAlbums", method = RequestMethod.GET)
    public ResultModel getAlbums(
            @ApiParam(value = "每页显示行数", required = false) @RequestParam(required = false, defaultValue = "10") Integer rows,
            @ApiParam(value = "页数", required = false) @RequestParam(required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "关键字查询", required = false) @RequestParam(required = false) String keywords
    ) throws Exception {
        // 分页条件
        resultModel.setRows(rows);
        resultModel.setPage(page);
        // 添加筛选条件
        resultModel.getSearchMap().put("keywords", keywords);

        albumService.getAlbums(resultModel);
        return resultModel;
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "上传文件", httpMethod = "POST", response = Album.class)
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ResultModel uploadImg(
            @ApiParam(value = "作品文件", required = false) @RequestParam MultipartFile file
    ) throws Exception{

        albumService.uploadImg(file, resultModel);
        return resultModel;
    }


}
